package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Drugs;
import com.ruoyi.system.domain.Prescription;
import com.ruoyi.system.domain.PrescriptionDetailed;
import com.ruoyi.system.service.IPrescriptionDetailedService;
import com.ruoyi.system.service.impl.DrugsServiceImpl;
import com.ruoyi.system.service.impl.PrescriptionServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 处方详情Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "处方详情模块")
@RestController
@RequestMapping("/system/PrescriptionDetailed")
public class PrescriptionDetailedController extends BaseController {
    @Autowired
    private IPrescriptionDetailedService prescriptionDetailedService;
    private class Wrap implements Serializable {
        PrescriptionDetailed PrescriptionDetailed;
        String DrugsId;
        String PrescriptionId;
        @JsonProperty("PrescriptionId")
        public String getPrescriptionId() {
            return PrescriptionId;
        }

        @JsonProperty("PrescriptionDetailed")
        public com.ruoyi.system.domain.PrescriptionDetailed getPrescriptionDetailed() {
            return PrescriptionDetailed;
        }

        @JsonProperty("DrugsId")
        public String getDrugsId() {
            return DrugsId;
        }
    }

    @Autowired
    DrugsServiceImpl drugsService;
    @Autowired
    PrescriptionServiceImpl prescriptionService;


    private Wrap convert(PrescriptionDetailed origin) {
        Wrap tempWrap = new Wrap();
        tempWrap.DrugsId = drugsService.getById(origin.getDrugsId()).getDrugsName();
        tempWrap.PrescriptionDetailed = origin;
        tempWrap.PrescriptionId = prescriptionService.getById(origin.getPrescriptionId()).getPrescriptionName();
        return tempWrap;
    }

    /**
     * 查询处方详情列表
     */
    @ApiOperation(value = "查询处方详情列表")
    @PreAuthorize("@ss.hasPermi('system:PrescriptionDetailed:list')")
    @GetMapping("/list")
    public Page<Wrap> list(PrescriptionDetailed entity, Page<PrescriptionDetailed> page) {

        QueryWrapper<PrescriptionDetailed> wrapper = new QueryWrapper<>();
        if (entity.getPrescriptionId() != null) {
            wrapper.eq("PrescriptionId", entity.getPrescriptionId());
        }
        Page<PrescriptionDetailed> old = prescriptionDetailedService.page(page, wrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<PrescriptionDetailed> object = old.getRecords();
        for (PrescriptionDetailed item : object) {
            if (prescriptionService.getById(item.getPrescriptionId()).getPrescriptionState() >= 3L) {
                wraps.add(convert(item));
            }
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;
    }

    /**
     * 导出处方详情列表
     */
    @ApiOperation(value = "导出处方详情列表")
    @PreAuthorize("@ss.hasPermi('system:PrescriptionDetailed:export')")
    @Log(title = "处方详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrescriptionDetailed prescriptionDetailed) {
        LambdaQueryWrapper<PrescriptionDetailed> queryWrapper = new LambdaQueryWrapper<>();
        List<PrescriptionDetailed> list = prescriptionDetailedService.list(queryWrapper);
        ExcelUtil<PrescriptionDetailed> util = new ExcelUtil<PrescriptionDetailed>(PrescriptionDetailed.class);
        util.exportExcel(response, list, "处方详情数据");
    }


    /**
     * 获取处方详情详细信息
     */
    @ApiOperation(value = "获取处方详情详细信息")
    @PreAuthorize("@ss.hasPermi('system:PrescriptionDetailed:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(prescriptionDetailedService.getById(id));
    }

    /**
     * 新增处方详情
     */
    @ApiOperation(value = "新增处方详情")
    @Log(title = "处方详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrescriptionDetailed prescriptionDetailed) {
        return toAjax(prescriptionDetailedService.save(prescriptionDetailed));
    }

    /**
     * 修改处方详情
     */
    @ApiOperation(value = "修改处方详情")
    @PreAuthorize("@ss.hasPermi('system:PrescriptionDetailed:edit')")
    @Log(title = "处方详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrescriptionDetailed prescriptionDetailed) {
        Prescription prescription = new Prescription();
        prescription.setId(prescriptionDetailed.getPrescriptionId());
        prescription.setPrescriptionState(4L);
        return toAjax(prescriptionDetailedService.updateById(prescriptionDetailed));
    }


    /**
     * 删除处方详情
     */
    @ApiOperation(value = "删除处方详情")
    @PreAuthorize("@ss.hasPermi('system:PrescriptionDetailed:remove')")
    @Log(title = "处方详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(prescriptionDetailedService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
